import * as homeApi from '@/api/homepage.js'

export default {
  namespaced: true,
  state() {
    return {
      homeList: [],
      bannerList: [],
      newGoodsList: [],
      popularList: [],
      hotList: [],
      goodsList: [],
      specialList: [],
      cartList: [],
      itemList: [],
    };
  },
  mutations: {
    GET_LIST(state, newValue) {
      state.homeList = newValue;
    },
    GET_BANNER(state, newValue) {
      state.bannerList = newValue;
    },
    GET_NEW_GOODS(state, newValue) {
      state.newGoodsList = newValue;
    },
    GET_POPULAR_GOODS(state, newValue) {
      state.popularList = newValue
    },
    GET_HOT_LIST(state, newValue) {
      state.hotList = newValue
    },
    GET_GOODS_LIST(state, newValue) {
      state.goodsList = newValue
    },
    GET_SPECIAL(state, newValue) {
      state.specialList = newValue
    },
    GET_CART_LIST(state, newValue) {
      state.cartList = newValue
    },
    GET_ITEM_LIST(state, newValue) {
      state.itemList = newValue
    }
  },
  actions: {
    async updateHeadList({ commit }) {
      await homeApi.getHomeList()
        .then((res) => {
          commit('GET_LIST', res.result)
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    async updateBannerList({ commit }) {
      await homeApi.getBanner()
        .then((res) => {
          commit('GET_BANNER', res.result)
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    async updateNewGoods({ commit }) {
      await homeApi.getNewGoods()
        .then((res) => {
          commit('GET_NEW_GOODS', res.result)
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    async updataPopularList({ commit }) {
      await homeApi.getPopularGoods()
        .then((res) => {
          commit('GET_POPULAR_GOODS', res.result)
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    async updateHotList({ commit }) {
      await homeApi.getHotList()
      .then((res) => {
        commit('GET_HOT_LIST', res.result)
      })
      .catch((err) => {
        return Promise.reject(err)
      })
    },
    async updateGoodsList({ commit }) {
      await homeApi.getGoodsList()
      .then((res) => {
        commit('GET_GOODS_LIST', res.result)
      })
      .catch((err) => {
        return Promise.reject(err)
      })
    },
    async updateSpecialList({ commit }) {
      await homeApi.getSpecial()
      .then((res) => {
        commit('GET_SPECIAL', res.result)
      })
      .catch((err) => {
        return Promise.reject(err)
      })
    },
    async updateCartList({ commit }) {
      await homeApi.getCartList()
      .then((res) => {
        commit('GET_CART_LIST', res.result)
      })
      .catch((err) => {
        return Promise.reject(err)
      })
    },
    async updateItemList({ commit }, id) {
      await homeApi.getGoodsListById(id)
      .then((res) => {
        commit('GET_ITEM_LIST', res.result)
      })
      .catch((err) => {
        return Promise.reject(err)
      })
    }
  },
}
